package com.dragon.modules.yuegou.utils;


import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 日期格式转换
 * Created by tangan on 2019/8/31.
 */
public class DateUtil {

    private static final String DATE_FORMATTER = "yyyy-MM-dd";
    private static final String DATETIME_FORMATTER = "yyyy-MM-dd HH:mm:ss";

    /**
     * @param time 待转换时间
     * @return 转换后时间
     * @throws Exception exception
     * @Description 将时间转换为该天最后时分秒23:59:59
     */
    public static LocalDateTime getFormaterEndTime(LocalDateTime time){
        try {
            if (null != time) {
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_FORMATTER);
                String formattedDateTime = time.format(formatter);
                formattedDateTime = formattedDateTime + " 23:59:59";
                DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern(DATETIME_FORMATTER);
                return LocalDateTime.parse(formattedDateTime, formatter2);
            }
            return null;
        }catch (Exception e){
            return null;
        }
    }

    public static LocalDateTime parseDate(String timeStr){
        return parseDate(timeStr,DATETIME_FORMATTER);
    }

    public static LocalDateTime parseDate(String timeStr,String pattern){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return LocalDateTime.parse(timeStr, formatter);
    }

    public static LocalDate parseLocalDate(String timeStr, String pattern){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return LocalDate.parse(timeStr, formatter);
    }

    public static LocalDateTime lastDate(LocalDateTime time,int days) {
    	try {
    		if ((null != time)) {
    			return LocalDateTime.now().plusDays(days);
    		}
    		return null;
    	} catch (Exception e){
            return null;
        }
    }

    public static String lastDate(String dateStr,int day,int month,int year,String pattern) {
        LocalDateTime d = LocalDateTime.now();
        try {
            if(PrivateStringUtils.isNotBlank(dateStr)){
                d = parseDate(dateStr, pattern);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        d = d.plusDays(day);
        d = d.plusDays(month);
        d = d.plusDays(year);
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern(pattern);
        return formatter2.format(d);
    }

    public static List<String> getBetweenDates(String start, String end) {
        LocalDate startDate = parseLocalDate(start,DATE_FORMATTER);
        LocalDate endDate = parseLocalDate(end,DATE_FORMATTER);

        List<String> result = new ArrayList<>();

        while (!startDate.isAfter(endDate)) {
            result.add(formatStandardDate(startDate));
            startDate = startDate.plusDays(1);
        }
        return result;
    }

    private static String formatStandardDate(LocalDate localDate){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_FORMATTER);
        return localDate.format(formatter);
    }

    public static String formatStandardDateTime(LocalDateTime localDateTime){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATETIME_FORMATTER);
        return localDateTime.format(formatter);
    }

    public static String formatStandardDateForPattern(LocalDate localDate, String pattern){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return localDate.format(formatter);
    }
    
    public static LocalDateTime formatLocalDateTimeByDate(Date date){      
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

}
